using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using AppPriceLive.Entities.Models;
using AppPriceLive.Entities.Utils;
using AppPriceLive.Common;

namespace AppPriceLive.DAL.Base
{
	public class CurrencyInfoDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CurrencyInfoDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CurrencyInfo table.
		/// </summary>
		public virtual void Insert(CurrencyInfoInfo currencyInfoInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyListId", currencyInfoInfo.CurrencyListId),
				new SqlParameter("@UpdateTime", currencyInfoInfo.UpdateTime),
				new SqlParameter("@Bid", currencyInfoInfo.Bid),
				new SqlParameter("@Ask", currencyInfoInfo.Ask),
				new SqlParameter("@UrlChart1Min", currencyInfoInfo.UrlChart1Min),
				new SqlParameter("@UrlChart10Min", currencyInfoInfo.UrlChart10Min),
				new SqlParameter("@UrlChart1Hour", currencyInfoInfo.UrlChart1Hour),
				new SqlParameter("@UrlChart1Day", currencyInfoInfo.UrlChart1Day),
				new SqlParameter("@UrlChart1Week", currencyInfoInfo.UrlChart1Week),
				new SqlParameter("@UrlChart1Month", currencyInfoInfo.UrlChart1Month),
				new SqlParameter("@UrlChart3Month", currencyInfoInfo.UrlChart3Month),
				new SqlParameter("@UrlChart6Month", currencyInfoInfo.UrlChart6Month),
				new SqlParameter("@UrlChart1Year", currencyInfoInfo.UrlChart1Year)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CurrencyInfo_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CurrencyInfo table.
		/// </summary>
		public virtual void Update(CurrencyInfoInfo currencyInfoInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyListId", currencyInfoInfo.CurrencyListId),
				new SqlParameter("@UpdateTime", currencyInfoInfo.UpdateTime),
				new SqlParameter("@Bid", currencyInfoInfo.Bid),
				new SqlParameter("@Ask", currencyInfoInfo.Ask),
				new SqlParameter("@UrlChart1Min", currencyInfoInfo.UrlChart1Min),
				new SqlParameter("@UrlChart10Min", currencyInfoInfo.UrlChart10Min),
				new SqlParameter("@UrlChart1Hour", currencyInfoInfo.UrlChart1Hour),
				new SqlParameter("@UrlChart1Day", currencyInfoInfo.UrlChart1Day),
				new SqlParameter("@UrlChart1Week", currencyInfoInfo.UrlChart1Week),
				new SqlParameter("@UrlChart1Month", currencyInfoInfo.UrlChart1Month),
				new SqlParameter("@UrlChart3Month", currencyInfoInfo.UrlChart3Month),
				new SqlParameter("@UrlChart6Month", currencyInfoInfo.UrlChart6Month),
				new SqlParameter("@UrlChart1Year", currencyInfoInfo.UrlChart1Year)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CurrencyInfo_Update", parameters);
        }

        /// <summary>
        /// Cap nhat gia mua vao ban ra
        /// </summary>
        /// <param name="currencyInfoInfo"></param>
        public virtual void Update_BigAndAsk(CurrencyInfoInfo currencyInfoInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyListId", currencyInfoInfo.CurrencyListId),
				new SqlParameter("@UpdateTime", currencyInfoInfo.UpdateTime),
				new SqlParameter("@Bid", currencyInfoInfo.Bid),
				new SqlParameter("@Ask", currencyInfoInfo.Ask)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CurrencyInfo_Update_BigAndAsk", parameters);
        }

		/// <summary>
		/// Deletes a record from the CurrencyInfo table by its primary key.
		/// </summary>
		public virtual void Delete(int? currencyListId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyListId", currencyListId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CurrencyInfo_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CurrencyInfo table.
		/// </summary>
		public virtual CurrencyInfoInfo Select(int? currencyListId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyListId", currencyListId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CurrencyInfo_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCurrencyInfoInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CurrencyInfo table.
		/// </summary>
		public List<CurrencyInfoInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CurrencyInfo_SelectAll"))
			{
				List<CurrencyInfoInfo> currencyInfoInfoList = new List<CurrencyInfoInfo>();
				while (dataReader.Read())
				{
					currencyInfoInfoList.Add(MakeCurrencyInfoInfo(dataReader));
				}

				return currencyInfoInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CurrencyInfo class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CurrencyInfoInfo MakeCurrencyInfoInfo(SqlDataReader dataReader)
		{
			CurrencyInfoInfo currencyInfoInfo = new CurrencyInfoInfo();
			currencyInfoInfo.CurrencyListId = SqlClientUtility.GetInt32(dataReader, DbConstants.CURRENCYINFO.CURRENCYLISTID);
			currencyInfoInfo.UpdateTime = SqlClientUtility.GetDateTime(dataReader, DbConstants.CURRENCYINFO.UPDATETIME);
			currencyInfoInfo.Bid = SqlClientUtility.GetDecimal(dataReader, DbConstants.CURRENCYINFO.BID);
			currencyInfoInfo.Ask = SqlClientUtility.GetDecimal(dataReader, DbConstants.CURRENCYINFO.ASK);
			currencyInfoInfo.UrlChart1Min = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART1MIN);
			currencyInfoInfo.UrlChart10Min = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART10MIN);
			currencyInfoInfo.UrlChart1Hour = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART1HOUR);
			currencyInfoInfo.UrlChart1Day = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART1DAY);
			currencyInfoInfo.UrlChart1Week = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART1WEEK);
			currencyInfoInfo.UrlChart1Month = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART1MONTH);
			currencyInfoInfo.UrlChart3Month = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART3MONTH);
			currencyInfoInfo.UrlChart6Month = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART6MONTH);
			currencyInfoInfo.UrlChart1Year = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYINFO.URLCHART1YEAR);

			return currencyInfoInfo;
		}

		#endregion
	}
}
